home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / wbstartlib.lha / WBStart / src / paths.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  5KB  |  208 lines

  1. /*
  2.  * paths.c  V2.0
  3.  *
  4.  * DOS Paths handling routines
  5.  *
  6.  * (c) 1991-96 Stefan Becker
  7.  *
  8.  */
  9.  
  10. /* Handler includes */
  11. #include "wbstart.h"
  12.  
  13. /* Local data structures */
  14. struct PathListEntry {
  15.  BPTR ple_Next; /* Next PathListEntry */
  16.  BPTR ple_Lock; /* Directory lock     */
  17. };
  18.  
  19. /* Local data */
  20. static struct PathListEntry *LoadPath;       /* Path for loading programs    */
  21. static struct PathListEntry *WorkbenchPath;  /* Copy of Workbench path       */
  22. static struct PathListEntry *OldProcessPath; /* Pointer to process' old path */
  23.  
  24. /* Scan load path for program and return directory lock */
  25. BPTR ScanLoadPath(char *name, BPTR currentdir)
  26. {
  27.  BPTR rc;
  28.  
  29.  /* Check current directory first */
  30.  if (rc = Lock(name, ACCESS_READ)) {
  31.  
  32.   DEBUGLOG(kprintf("Program found in current directory\n");)
  33.  
  34.   /* Program found, unlock it */
  35.   UnLock(rc);
  36.  
  37.   /* Return current directory as home directory */
  38.   rc = currentdir;
  39.  
  40.  } else {
  41.   struct PathListEntry *current = LoadPath;
  42.  
  43.   /* Program not found, scan path list */
  44.   while (current) {
  45.    BPTR filelock;
  46.  
  47.    DEBUGLOG(kprintf("Scanning load path 0x%08lx\n", current->ple_Lock);)
  48.  
  49.    /* Go to directory */
  50.    CurrentDir(current->ple_Lock);
  51.  
  52.    /* Program in this directory? */
  53.    if (filelock = Lock(name, ACCESS_READ)) {
  54.  
  55.     /* Yes, unlock it */
  56.     UnLock(filelock);
  57.  
  58.     /* Set return code */
  59.     rc = current->ple_Lock;
  60.  
  61.     /* Leave loop */
  62.     break;
  63.    }
  64.  
  65.    /* Next entry */
  66.    current = BADDR(current->ple_Next);
  67.   }
  68.  
  69.   /* Go back to current directory */
  70.   CurrentDir(currentdir);
  71.  }
  72.  
  73.  /* Return home directory lock */
  74.  return(rc);
  75. }
  76.  
  77. /* Free a path list */
  78. static void FreePathList(struct PathListEntry *list)
  79. {
  80.  struct PathListEntry *current;
  81.  struct PathListEntry *next    = list;
  82.  
  83.  /* Scan list */
  84.  while (current = next) {
  85.  
  86.   /* Save pointer to next entry */
  87.   next = BADDR(current->ple_Next);
  88.  
  89.   /* Release lock */
  90.   UnLock(current->ple_Lock);
  91.  
  92.   /* Free current path list entry */
  93.   FreeMemory(current);
  94.  }
  95. }
  96.  
  97. /* Copy a path list */
  98. static struct PathEntryList *CopyPathList(struct PathListEntry *orig)
  99. {
  100.  struct PathListEntry *head    = NULL; /* Head of copied path list          */
  101.  struct PathListEntry *current = NULL; /* Current entry in copied path list */
  102.  struct PathListEntry *next    = NULL; /* Next allocated path list entry    */
  103.  
  104.  /* Scan original path list */
  105.  while (orig) {
  106.  
  107.   /* Allocate memory for path list entry */
  108.   if ((next != NULL) ||
  109.       (next = AllocateMemory(sizeof(struct PathListEntry)))) {
  110.  
  111.    /* Duplicate directory lock */
  112.    if (next->ple_Lock = DupLock(orig->ple_Lock)) {
  113.  
  114.     /* Lock duplicated, clear pointer to next node */
  115.     next->ple_Next = NULL;
  116.  
  117.     /* List empty? */
  118.     if (head)
  119.  
  120.      /* No, append new entry after current entry */
  121.      current->ple_Next = MKBADDR(next);
  122.  
  123.     else
  124.  
  125.      /* Yes, new entry is head of list, save pointer */
  126.      head = next;
  127.  
  128.     /* Move current pointer */
  129.     current = next;
  130.  
  131.     /* Clear next pointer. A new entry must be allocated in the next round. */
  132.     next = NULL;
  133.    }
  134.  
  135.    /* Couldn't allocate memory for path list entry */
  136.   } else {
  137.  
  138.    /* Free path list */
  139.    FreePathList(head);
  140.  
  141.    /* Clear pointer */
  142.    head = NULL;
  143.  
  144.    /* Leave loop */
  145.    break;
  146.   }
  147.  
  148.   /* Get next entry in original path list */
  149.   orig = BADDR(orig->ple_Next);
  150.  }
  151.  
  152.  /* Next path list entry already allocated? Free it! */
  153.  if (next) FreeMemory(next);
  154.  
  155.  /* Return pointer to head of copied path list */
  156.  return(head);
  157. }
  158.  
  159. /* Copy path from Workbench process and install it as our process' path */
  160. void InstallWorkbenchPath(void)
  161. {
  162.  struct CommandLineInterface *cli = Cli();
  163.  struct Process *wbproc;
  164.  
  165.  /* Get pointer to our old path */
  166.  OldProcessPath = (struct PathList *) BADDR(cli->cli_CommandDir);
  167.  
  168.  /* Set this path as default load path */
  169.  LoadPath = OldProcessPath;
  170.  
  171.  /* Clear pointer to Workbench path */
  172.  WorkbenchPath = NULL;
  173.  
  174.  /* Find Workbench task and make sure it is a DOS process */
  175.  if ((wbproc = (struct Process *) FindTask("Workbench")) &&
  176.      (wbproc->pr_Task.tc_Node.ln_Type == NT_PROCESS)) {
  177.   struct CommandLineInterface *wbcli;
  178.  
  179.   /* Make sure it is a CLI process and copy the path */
  180.   if ((wbcli = BADDR(wbproc->pr_CLI)) &&
  181.       (WorkbenchPath = CopyPathList((struct PathList *)
  182.                                     BADDR(wbcli->cli_CommandDir)))) {
  183.  
  184.    DEBUGLOG(kprintf("Workbench path copied and installed.\n");)
  185.  
  186.    /* Path successfully copied, install it in our process */
  187.    cli->cli_CommandDir = MKBADDR(WorkbenchPath);
  188.  
  189.    /* Use workbench path as load path */
  190.    LoadPath = WorkbenchPath;
  191.   }
  192.  }
  193. }
  194.  
  195. /* Remove Workbench path */
  196. void RemoveWorkbenchPath(void)
  197. {
  198.  /* Copy of Workbench path valid? */
  199.  if (WorkbenchPath) {
  200.  
  201.   /* Yes, reinstall our old path */
  202.   Cli()->cli_CommandDir = MKBADDR(OldProcessPath);
  203.  
  204.   /* Free Workbench Path */
  205.   FreePathList(WorkbenchPath);
  206.  }
  207. }
  208.